package com.djsearch.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.djsearch.R;
import com.djsearch.adapter.SearchWayPopuAdapter;
import com.djsearch.controller.JSONResp;

/**
 * 选择城市
 * 
 * @author yx
 * 
 */
public class ChooseCityActivity extends BaseActivity {

	private Context mContext;
	private ListView cityList;
	private Button queDingBtn,btnBack;
	private ImageView searchImg;
	private EditText editText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.choose_city_layout);
		mContext = this;
		// 初始化主界面
		initView();
		// 初始化主界面相关点击事件
		initOnClick();
		//
		initHandler();
	}

	@Override
	void initHandler() {
		// TODO Auto-generated method stub

	}

	@Override
	void initView() {
		// TODO Auto-generated method stub

		cityList = (ListView)findViewById(R.id.list);
		cityList.setAdapter(new SearchWayPopuAdapter(this,list, listTag));
//		queDingBtn = (Button)findViewById(R.id.btnBack);
		searchImg = (ImageView)findViewById(R.id.searchImg);
		editText = (EditText)findViewById(R.id.init_name);
		btnBack = (Button)findViewById(R.id.btnBack);
		
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub

		btnBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				startActivity(mContext, null, 1);
				finish();
				
			}
		});
		
		searchImg.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (editText.getText().toString().equals("")) {
					startAnimation(editText);
				} else {
					Intent intent = new Intent();                
				    Bundle bundle = new Bundle();    
				    bundle.putString("NAME",editText.getText().toString());     
				    intent.putExtras(bundle);                                      
			        ChooseCityActivity.this.setResult(RESULT_OK,intent);   
			        ChooseCityActivity.this.finish();  
			        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
				}
				
			}
		});
		
		cityList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();                
				Bundle bundle = new Bundle();    
				bundle.putString("NAME",list.get(position));     
				intent.putExtras(bundle);                                      
			    ChooseCityActivity.this.setResult(RESULT_OK,intent);   
			    ChooseCityActivity.this.finish();  
			    overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
			}
		});
	}

	/**
	 * 点击返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(mContext, null, 1);
		}
		return false;
		
	}
	
	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub
		
	}

}
